package longrise.phone.com.bjjt_jyb.compensate;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.Constant.URLConstant;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;

public class SingleCarOver extends LFView implements View.OnClickListener {
    private BaseApplication mApplication;
    private Context context = null;
    private View view = null;
    private LinearLayout hn_btn_back;
    private Button bt_singlecarover;
    private Dialog processDialog;
    private String appcaseno;


    public SingleCarOver(Context c,String appcaseno) {
        super(c);
        this.context = c;
        mApplication = (BaseApplication) c.getApplicationContext();
        this.appcaseno=appcaseno;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.viewsinglecarover, null);
        bt_singlecarover = (Button) view.findViewById(R.id.bt_singlecarover);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        hn_btn_back.setVisibility(View.INVISIBLE);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("温馨提示");
    }

    private void regEvent(boolean b) {
        if (null != bt_singlecarover) {
            bt_singlecarover.setOnClickListener(b ? this : null);
        }
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;
            case R.id.bt_singlecarover:
                //调取发送短信的接口
                sendMessage();
                break;
        }
    }
    /**
     * 案件定完发送短信
     */
    private void sendMessage() {
        try {
            processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
            EntityBean bean = new EntityBean();
            bean.set("carbeans","");
            bean.set("appcaseno",appcaseno);
            LoadDataManagerCar.getInstance(context).callService(null,
                    mApplication.getServerUrl(), URLConstant.JJAPPSENDDX, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                        @Override
                        public void onSuccess(String key, String service, Object result) {
                            if (null != processDialog)
                            {
                                if (processDialog.isShowing())
                                {
                                    processDialog.dismiss();
                                }
                            }
                            EntityBean bean = (EntityBean) result;
                            String restate = bean.getString("restate");
                            if ("1".equals(restate)) {
                                closeAllForm();
                                HomePageView homepageview = new HomePageView(context);
                                FrameworkManager.getInstance().showNewForm(context, homepageview);
                            } else {
                                UiUtil.showToast(context,"发送短信失败，请重试");
                            }
                        }

                        @Override
                        public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                            if (null != processDialog)
                            {
                                if (processDialog.isShowing())
                                {
                                    processDialog.dismiss();
                                }
                            }
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        }

                        @Override
                        public void onFinished(String key, String service) {
                            if (null != processDialog)
                            {
                                if (processDialog.isShowing())
                                {
                                    processDialog.dismiss();
                                }
                            }
                        }
                    });
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

}
